package com.aceleeyy.apb.constants;

/**
 * 过滤条件匹配
 *
 * eq                   | Equal
 * ne                   | Not equal
 * gt                   | Greater than
 * ge                   | Greater than or equa
 * lt                   | Less than
 * le                   | Less than or equal
 * startswith           | startswith
 * endswith             | endswith
 * contains             | contains
 * isnull               | isnull
 * isnotnull            | isnotnull
 * Logical Operators    |
 * and                  | Logical and
 * or                   | Logical or
 * not                  | Logical negation
 * Grouping Operators   |
 * ( )                  | Precedence grouping
 *
 * @author Ace Lee
 * @date 2019/3/5 16:46
 */
public enum FilterMatches {

    F_EQ(" eq ", " = "),
    F_NE(" ne ", " != "),
    F_GT(" gt ", " > "),
    F_GE(" ge ", " >= "),
    F_LT(" lt ", " < "),
    F_LE(" le ", " <= "),
    F_STARTWITH(" startswith ", " like "),
    F_ENDSWITH(" endswith ", " like "),
    F_CONTAINS(" contains ", " like "),
    F_ISNULL(" isnull ", " is null "),
    F_ISNOTNULL(" isnotnull ", " is not null "),
    F_AND(" and ", " and "),
    F_OR(" or ", " or "),
    F_NOT(" not ", " not ");

    /**
     * 过滤代码
     */
    private String filterCode;
    /**
     * 匹配语法
     */
    private String matchGrammar;

    public String getFilterCode() {
        return filterCode;
    }


    public String getMatchGrammar() {
        return matchGrammar;
    }


    FilterMatches(String filterCode, String matchGrammar) {
        this.filterCode = filterCode;
        this.matchGrammar = matchGrammar;
    }


    public static String explain(String filterCode) {
        for (FilterMatches filterMatches : FilterMatches.values()) {
            if (filterMatches.filterCode.equals(filterCode)) {
                return filterMatches.matchGrammar;
            }
        }
        return filterCode;
    }

    public static String parseFilter(String filter){
        return filter.replaceAll(FilterMatches.F_EQ.getFilterCode(), FilterMatches.F_EQ.getMatchGrammar())
                .replaceAll(FilterMatches.F_NE.getFilterCode(), FilterMatches.F_NE.getMatchGrammar())
                .replaceAll(FilterMatches.F_GT.getFilterCode(), FilterMatches.F_GT.getMatchGrammar())
                .replaceAll(FilterMatches.F_GE.getFilterCode(), FilterMatches.F_GE.getMatchGrammar())
                .replaceAll(FilterMatches.F_LT.getFilterCode(), FilterMatches.F_LT.getMatchGrammar())
                .replaceAll(FilterMatches.F_LE.getFilterCode(), FilterMatches.F_LE.getMatchGrammar())
                .replaceAll(FilterMatches.F_ISNULL.getFilterCode(), FilterMatches.F_ISNULL.getMatchGrammar())
                .replaceAll(FilterMatches.F_ISNOTNULL.getFilterCode(), FilterMatches.F_ISNOTNULL.getMatchGrammar());
    }

    public static boolean containsStarswith(String filter){
        return filter.contains(FilterMatches.F_STARTWITH.getFilterCode());
    }

    public static boolean containsEndswith(String filter){
        return filter.contains(FilterMatches.F_ENDSWITH.getFilterCode());
    }

    public static boolean containsContains(String filter){
        return filter.contains(FilterMatches.F_CONTAINS.getFilterCode());
    }

    public static String parseStarswith(String filter){
        return filter.replaceAll(FilterMatches.F_STARTWITH.getFilterCode(), FilterMatches.F_STARTWITH.getMatchGrammar());
    }

    public static String parseEndswith(String filter){
        return filter.replaceAll(FilterMatches.F_ENDSWITH.getFilterCode(), FilterMatches.F_ENDSWITH.getMatchGrammar());
    }

    public static String parseContains(String filter){
        return filter.replaceAll(FilterMatches.F_CONTAINS.getFilterCode(), FilterMatches.F_CONTAINS.getMatchGrammar());
    }

}
